package test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 日期工具类
 * @author <a href="mailto:huzhaopan@163.com">Jesun</a>
 * @since 2011-6-13 上午09:07:06
 */
public final class DateUtil {
    /**日期格式*/
    private static DateFormat df=new SimpleDateFormat("yyyyMMdd");
    /**
     * 取得当月第一天
     * @return 当月第一天字符串
     */
    public static String getFirstDayOfMonth(){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.DATE,1);
        return df.format(c.getTime());
    }
    /**
     * 取得当月最后一天
     * @return 当月最后一天字符串
     */
    public static String getLastDayOfMonth(){
        Calendar c=Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DATE,1);
        c.add(Calendar.DATE, -1);
        return df.format(c.getTime());
    }
    /**
     * 取得当日日期
     * @return
     */
    public static String getCurDay(){
        return df.format(new java.util.Date());
    }
    /**
     * 取当前日期的前6天的日期
     * @return
     */
    public static String getPreWeekDay(){
        Calendar c=Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH,-6);
        return df.format(c.getTime());
    }
    /**
     * 取得当前年度的第一天
     * @return
     */
    public static String getFirstDayOfYear(){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.MONTH,0);
        c.set(Calendar.DATE, 1);
        return df.format(c.getTime());
    }
    /**
     * 取得当前年度的最后一天
     * @return
     */
    public static String getLastDayOfYear(){
        Calendar c=Calendar.getInstance();
        c.add(Calendar.YEAR, 1);
        c.set(Calendar.MONTH,0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.DATE,-1);
        return df.format(c.getTime());
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(getFirstDayOfMonth());
        System.out.println(getLastDayOfMonth());
        System.out.println(getPreWeekDay());
        System.out.println(getCurDay());
        System.out.println(getFirstDayOfYear());
        System.out.println(getLastDayOfYear());
    }
}
